package cn.com.bob.library.controller;

import cn.com.bob.library.controller.ex.PermissionException;
import cn.com.bob.library.entity.Book;
import cn.com.bob.library.entity.User;
import cn.com.bob.library.service.UserService;
import cn.com.bob.library.util.JsonResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author Bob
 * @Version：1.0
 */
@RestController
@RequestMapping("user")
public class UserController extends BaseController{
    @Autowired
    private UserService userService;

    /**
     * 用户注册
     * @param user:
     * @return JsonResult<Void>
     */
    @RequestMapping("register")
    @ResponseBody
    public JsonResult<Void> register(User user){
        userService.addUser(user,user.getUsername());
        return new JsonResult<>(OK);
    }

    /**
     * 用户登录
     * @param username:
     * @param password:
     * @return JsonResult<User>
     */
    @RequestMapping("login")
    @ResponseBody
    public JsonResult<User> login(String username,String password,HttpSession session){
        User res = userService.login(username,password);
        session.setAttribute("username",res.getUsername());
        session.setAttribute("userId",res.getUserId());
        session.setAttribute("userPermission",res.getUserPermission());
        return new JsonResult<>(OK,res);
    }

    @GetMapping("/getAllUsers")
    @ResponseBody
    public List<User> getAllUsers() {
        return userService.queryAllUsers();
    }

    @GetMapping("/getNormalUsers")
    @ResponseBody
    public List<User> getNormalUsers() {

        return userService.queryAllNormalUsers();
    }

    @GetMapping("/getBookManagers")
    @ResponseBody
    public List<User> getBookManagers() {
        return userService.queryAllBookManagers();
    }

    @GetMapping("/getAllManagers")
    @ResponseBody
    public List<User> getAllManagers() {
        return userService.queryAllManagers();
    }

    @DeleteMapping("/deleteUser")
    public JsonResult<Void> deleteUser(@RequestBody User user,HttpSession session) {
        //System.out.println(1);
        // 从请求体中获取要删除的用户对象
        // 执行删除操作，例如调用适当的服务层方法
        Integer permission = getPermissionFromSession(session);
        if(permission != 2){
            throw new PermissionException("权限不够");
        }
        System.out.println(userService.deleteUserByUsername(user.getUsername()));
        // ...
        // 返回适当的响应
        return new JsonResult<>(OK);
    }

    @PutMapping("/updateUser")
    public JsonResult<Void> updateUser(@RequestBody User user,HttpSession session){
        System.out.println(1);
        userService.updateUserByUsername(user,getUsernameFromSession(session));
        return new JsonResult<>(OK);

    }

    @PostMapping("/addUser")
    public JsonResult<Void> addUser(@RequestBody User user,HttpSession session){
        userService.addUser(user,getUsernameFromSession(session));
        return new JsonResult<>(OK);
    }

    /**
     * 获取某用户借阅的所有图书
     * @param session:
     * @return JsonResult<Void>
     */
    @GetMapping("/getBorrowBooks")
    public List<Book> getBorrowBooks(HttpSession session){
        return userService.getBorrowBooks(getUidFromSession(session));
    }

    /**
     * 对图书名进行查询
     * @param title:
     * @param session:
     * @return List<Book>
     */
    @GetMapping("/getBorrowBooksByTitle")
    public List<Book> getBorrowBooksByTitle(String title,HttpSession session){
        return userService.getBorrowBooksByTitle(title,getUidFromSession(session));
    }

    /**
     * 归还图书
     * @param bookId:
     * @param session:
     * @return JsonResult<Void>
     */
    @PostMapping("/returnBook")
    public JsonResult<Void> returnBook(@RequestBody Book book,HttpSession session){
        userService.returnBook(getUidFromSession(session),book.getBookId());
        return new JsonResult<>(OK);
    }
}
